# -*- coding: utf-8 -*-
# @Time    : 2023/9/27 21:40
# @Author  : Z.J. Zhang
# @Email   : zijingzhang@mail.ustc.edu.cn
# @File    : IHEPLGB.py
# @Software: PyCharm

import matplotlib
import numpy
import pandas

matplotlib.use('tkagg')
import matplotlib.pyplot as plt

_ = plt
df = pandas.read_excel(r"F:\GPTfiles\LGB\integration\pys\IHEPdataprocessing\data\LGB2数据汇总-截取-20231012-3.xlsx",
                       # skiprows=1,
                       # header=[0, 1],
                       # index_col=0
                       )
plt.figure()
plt.plot(df['z'], df[0], label='before')
# df.loc[540:640, df.columns[1:]] *= 1.0045
# df.loc[422:540, df.columns[1:]] *= 0.9994
# df.loc[422:, df.columns[1:]] *= 1.0045
# df.loc[:422, df.columns[1:]] *= 0.9987

plt.plot(df['z'], df[0], label='after')
plt.legend()

# 打印台阶中心磁感应强度
step_center_z = (numpy.array(list(range(5))) + +0.5) * 230
print('z/mm\tB/Gs')
for z in (step_center_z):
    print("%.2f\t%.5f" % (z, df[0][df['z'] >= z].iloc[-1]))



def measured_data_to_GPT_readable_ascii(df: pandas.DataFrame):
    """

    :param df:
    :return: unit in SI (m and T)
    """
    resdf = pandas.DataFrame(columns='X	Y	Z	BX	BY	BZ'.split('\t'))
    fake_ys_mm = [-1, 0, 1]
    for y in fake_ys_mm:
        for x in df.columns[1:]:
            old_index = resdf.index.to_list()
            resdf = resdf.reindex(list(range(len(old_index) + len(df))))

            resdf.loc[len(old_index):, ['Z', 'BY']] = df[['z', x]].values
            resdf.loc[len(old_index):, ['X', 'Y']] = x, y

            # resdf[['X','Y']][len(old_index):] = x,y
            # resdf['Y'][len(old_index):] = y
    resdf.fillna(0, inplace=True)
    resdf['X Y Z'.split(' ')] *= 1e-3
    resdf['BX BY BZ'.split(' ')] *= 1e-4

    return resdf


GPT_readable_ascii = measured_data_to_GPT_readable_ascii(df)
GPT_readable_ascii.to_csv(r'../..\Bdata.txt', index=False, sep='\t')
